package sdau.hjk.util;

import org.springframework.beans.factory.annotation.Autowired;
import sdau.hjk.service.Impl.Covid19DataService;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;
import java.util.Timer;


/**
 *@version 1.0
 *@author: huangjiakai
 *@create:2021−04-20 19:54
 *@className:sdau.hjk.util.TimeUtils
 *@description: TODO
 */
public class TimeUtils implements ServletContextListener {
    @Autowired
    private Covid19DataService dataService;
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        System.out.println("定时发送Xml信息监听--已关闭！");
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.set(year, month, day, 6, 00, 00);
        Date defaultdate = calendar.getTime();
        Date sendDate = null;
        if (defaultdate.before(new Date())) {
            calendar.add(Calendar.DATE, 1);
            sendDate = calendar.getTime();
        }else {
            sendDate = defaultdate;
        }

        Timer dTimer = new Timer();
        dTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("每日任务已经执行");
                dataService.updateData();
            }
        }, sendDate, 24 * 60 * 60 * 1000);// 定时24小时：24 * 60 * 60 * 1000
        System.out.println("每日定时发送Xml信息监听--已启动！");
    }

}

